<template>
  <div>
    <div class="search">
      <el-form
        ref="queryForm"
        class="queryForm"
        :inline="true"
        :model="formInline"
        label-width="100"
      >
        <el-row :gutter="20">
          <el-col
            :span="8"
          >
            <el-form-item
              label="文件名称:"
              class="formItem"
            >
              <el-input
                v-model="formInline.fileName"
                placeholder="请输入文件名"
                clearable
                class="w100"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="授权渠道:"
              class="formItem"
            >
              <el-select
                v-model="formInline.comAccountBank"
                placeholder="请选择授权渠道"
                clearable
                class="w100"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item,index in bankOptions"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="上传人:"
              class="formItem"
            >
              <el-input
                v-model="formInline.userName"
                placeholder="请输入上传人"
                clearable
                class="w100"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col
            :span="8"
          >
            <el-form-item
              label="解析状态:"
              class="formItem"
            >
              <el-select
                v-model="formInline.status"
                placeholder="请选择状态"
                class="w100"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item,index in statusOptions"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="导入时间:"
              class="formItem"
            >
              <el-date-picker
                class="w100"
                v-model="formInline.timeRange"
                type="daterange"
                range-separator="至"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleSearchList"
              >搜索</el-button>
              <el-button
                icon="el-icon-refresh"
                @click="handleReset"
              >清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="list">
        <el-button
          size="small"
          icon="el-icon-upload"
          type="primary"
          class="uplaod"
          @click="openAdd"
        >导入文件</el-button>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          :header-cell-style="{background:'#eff7fd'}"
          ref="table"
          v-loading="loading"
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="文件名称"
            prop="fileName"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="授权渠道"
            prop="comAccountBank"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{scope.row.comAccountBank | columnFilter(bankOptions)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="上传人"
            width="150"
            prop="userName"
            align="center"
          ></el-table-column>
          <el-table-column
            label="解析状态"
            prop="status"
            width="150"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.status !== 3">{{scope.row.status | columnFilter(statusOptions)}}</span>
              <span v-if="scope.row.status == 3">{{scope.row.remark}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="子文件总数"
            width="100"
            prop="allNum"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{scope.row.status == 2 || scope.row.status == 3 ? scope.row.allNum : '-'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="成功数"
            width="100"
            prop="sucNum"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{scope.row.status == 2 || scope.row.status == 3 ? scope.row.sucNum : '-'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="失败数"
            width="100"
            prop="errNum"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{scope.row.status == 2 || scope.row.status == 3 ? scope.row.errNum : '-'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="授权企业数"
            width="100"
            prop="allNum"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{scope.row.status == 2 || scope.row.status == 3 ? scope.row.qyNum : '-'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="导入时间"
            prop="createTime"
            align="center"
          ></el-table-column>
          <el-table-column
            label="操作"
            width="100"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                v-if="(scope.row.status == 2 || scope.row.status == 3) && scope.row.allNum != '0'"
                @click.stop="handleCheck(scope.row)"
              >查看</el-button>
              <span v-else>-</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @size-change="sizeChange"
            @current-change="pageChange"
            :current-page="pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
        <div class="footer-btn">
            <el-button
              type="primary"
              @click="goBack"
            > 返回 </el-button>
          </div>
      </div>
    </div>
    
    <el-dialog
      title="导入文件"
      :visible.sync="addBox"
      width="30%"
      center
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="message">
        <div class="module">
          <el-tabs v-model="basicinfo">
            <el-tab-pane
              label="授权渠道"
              name="first"
            >
              <span>授权渠道：</span>
              <el-select
                v-model="bank"
                clearable
                placeholder="请选择授权渠道"
              >
                <el-option
                  v-for="item in bankOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="module">
          <el-tabs v-model="basicinfo">
            <el-tab-pane
              label="导入文件"
              name="first"
            >
              <el-upload
                action=""
                :file-list="fileList"
                :auto-upload="false"
                :limit="1"
                ref="upload"
                accept=".zip"
                :on-remove="handleRemove"
                :on-change="handelChange"
              >
                <el-button
                  size="small"
                  icon="el-icon-upload"
                  type="primary"
                >点击上传</el-button>
                <div
                  slot="tip"
                  class="el-upload__tip"
                >只能上传zip文件，且上传文件大小不能超过 100MB!</div>
              </el-upload>
            </el-tab-pane>
          </el-tabs>
        </div>
        <div class="dialog-footer">
          <el-button
            type="primary"
            @click="submit"
            :loading="submitLoading"
          >确定</el-button>
          <el-button
            @click="addBox = false"
            type="primary"
            plain
            :disabled="submitLoading"
          >取消</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      title="查看"
      :visible.sync="checkBox"
      width="70%"
      center
    >
      <el-form
        ref="checkForm"
        class="queryForm"
        :inline="true"
        :model="checkformInline"
        label-width="150"
      >
        <el-row :gutter="20">
          <el-col
            :span="7"
            :offset="1"
          >
            <el-form-item
              label="子文件名称:"
              class="formItem"
            >
              <el-input
                v-model="checkformInline.subName"
                placeholder="请输入文件名"
                clearable
                class="w100"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item
              label="解析状态:"
              class="formItem"
            >
              <el-select
                v-model="checkformInline.status"
                placeholder="请选择状态"
                clearable
                class="w100"
              >
                <el-option
                  :label="item.label"
                  :value="item.value"
                  v-for="item,index in checkStatusOptions"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handelCheckSearchList"
              >搜索</el-button>
              <el-button
                icon="el-icon-refresh"
                @click="handlecheckReset"
              >清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        :data="checkData"
        border
        style="width: 100%"
        :header-cell-style="{background:'#eff7fd'}"
        ref="table"
        v-loading="checkLoading"
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <span>{{(checkPageNum - 1) * checkPageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="子文件名称"
          prop="subName"
          align="center"
          width="300"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          label="解析状态"
          prop="status"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{scope.row.status | columnFilter(checkStatusOptions)}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="解析时间"
          prop="createTime"
          align="center"
        ></el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          align="center"
        ></el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="checkSizeChange"
          @current-change="checkPageChange"
          :current-page="checkPageNum"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="checkPageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="checkTotal"
        >
        </el-pagination>
      </div>
    </el-dialog>
    
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import axios from 'axios'
import { getBatchList, listById } from '@/api/manager.js'
export default {
  props: ['bankOptions'],
  computed: {
    ...mapGetters(['token']),
  },
  data() {
    return {
      formInline: {
        timeRange: [],
      },
      statusOptions: [
        { label: '待解析', value: 0 },
        { label: '解析中', value: 1 },
        { label: '解析完成', value: 2 },
        { label: '解析失败', value: 3 },
        { label: '提交中', value: 4 },
      ],
      checkStatusOptions: [
        { label: '解析完成', value: 2 },
        { label: '解析失败', value: 3 },
        { label: '提交中', value: 4 },
      ],
      tableData: [],
      loading: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      fileList: [],
      basicinfo: 'first',
      addBox: false,
      bank: '',
      submitLoading: false,
      checkBox: false,
      checkformInline: {},
      checkBoxId: '',
      checkData: [],
      checkLoading: false,
      checkPageNum: 1,
      checkPageSize: 10,
      checkTotal: 0,
    }
  },
  created() {
    this.handleSearch()
  },
  filters: {
    columnFilter(val, option) {
      if (!val && val !== 0) return ''
      return option
        .filter((item) => item.value == val)
        .map((ite) => ite.label)[0]
    },
  },
  methods: {
    handleSearch() {
      let formInline = this.formInline
      let params = {
        ...formInline,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }
      if (formInline.timeRange.length > 0) {
        params.startTime = formInline.timeRange[0]
        params.endTime = formInline.timeRange[1]
        delete params['timeRange']
      }
      this.loading = true
      getBatchList(params).then((res) => {
        if (res.status) {
          this.loading = false
          this.tableData = res.data.records
          this.total = Number(res.data.total)
        } else {
          this.$message.error(`${res.msg}`)
        }
      })
    },
    sizeChange(val) {
      this.pageSize = val
      this.handleSearch()
    },
    pageChange(val) {
      this.pageNum = val
      this.handleSearch()
    },
    handleSearchList() {
      this.pageNum = 1
      this.pageSize = 10
      this.handleSearch()
    },
    //清空
    handleReset() {
      this.pageNum = 1
      this.pageSize = 10
      this.formInline = {
        timeRange: [],
      }
      this.handleSearch()
    },
    //查看
    handleCheck(row) {
      this.checkBoxId = row.id
      this.checkBox = true
      this.handlecheckReset()
    },
    handlecheckReset() {
      this.checkformInline = {}
      this.checkPageNum = 1
      this.checkPageSize = 10
      this.handelCheckSearch()
    },
    handelCheckSearchList() {
      this.checkPageNum = 1
      this.checkPageSize = 10
      this.handelCheckSearch()
    },
    handelCheckSearch() {
      let checkformInline = this.checkformInline
      let params = {
        ...checkformInline,
        id: this.checkBoxId,
        pageNum: this.checkPageNum,
        pageSize: this.checkPageSize,
      }
      this.checkLoading = true
      listById(params).then((res) => {
        if (res.status) {
          this.checkLoading = false
          this.checkData = res.data.records
          this.checkTotal = Number(res.data.total)
        } else {
          this.$message.error(`${res.msg}`)
        }
      })
    },
    checkSizeChange(val) {
      this.checkPageSize = val
      this.handelCheckSearch()
    },
    checkPageChange(val) {
      this.checkPageNum = val
      this.handelCheckSearch()
    },
    openAdd() {
      this.fileList = []
      this.bank = ''
      this.addBox = true
    },
    handelChange(file, fileList) {
      const fileFamart = file.name.split('.')[file.name.split('.').length - 1]
      if (fileFamart !== 'zip') {
        this.$message.warning('必须上传zip格式的文件!')
        this.$refs.upload.handleRemove(file)
        return
      }
      const sizeLimit = file.size / 1024 / 1024 > 100
      if (sizeLimit) {
        this.$message.warning('上传文件大小不能超过 100MB!')
        this.$refs.upload.handleRemove(file)
        return
      }
      if (!sizeLimit && fileFamart === 'zip') {
        this.fileList = fileList
      }
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    submit() {
      if (!this.bank) {
        this.$message({
          type: 'error',
          message: `请选择授权渠道`,
        })
        return
      }
      if (this.fileList.length == 0) {
        this.$message({
          type: 'warning',
          message: `请先选择需要上传的文件`,
        })
        return
      }
      let formData = new FormData() // FormData 对象
      this.fileList.forEach((file) => {
        formData.append('file', file.raw)
      })
      let url =
        process.env.VUE_APP_BASE_API +
        '/qyxx/authorize/qysqxyBatch/uploadZipFile?comAccountBank=' +
        this.bank
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data',
          TOKEN: this.token,
        },
      }
      this.submitLoading = true
      axios
        .post(url, formData, config)
        .then((res) => {
          if (res.data.status) {
            this.$message({
              type: 'success',
              message: '操作成功!',
            })
            this.handleSearch()
            this.addBox = false
            this.submitLoading = false
          } else {
            this.submitLoading = false
            this.$message({
              type: 'error',
              message: `${res.data.msg}`,
            })
          }
        })
        .catch((error) => {
          this.submitLoading = false
          this.$message({
            type: 'error',
            showClose: true,
            duration: 3000,
            message: error,
          })
        })
    },
    goBack() {
      this.$emit('goBack')
    },
  },
}
</script>

<style lang="scss" scoped>
.search {
  padding: 20px 0 20px 20px;
  background: #fff;
  border-radius: 10px;
}
.formItem {
  width: 100%;
  /deep/.el-form-item__content {
    width: calc(100% - 125px);
    /deep/.el-select,
    /deep/.el-cascader {
      width: 100%;
    }
    .w100 {
      width: 100% !important;
    }
  }
}
.uplaod {
  margin-bottom: 20px;
}
.list {
  background: #fff;
  .pagination {
    width: 100%;
    margin-top: 10px;
    display: flex;
    justify-content: center;
  }
}
.footer-btn {
  margin-top: 20px;
  width: 100%;
  text-align: center;
}
.dialog-footer {
  text-align: center;
  margin-top: 20px;
}
</style>